//
//  RACSignalViewController.m
//  RACDemo
//
//  Created by he.xu on 2017/12/29.
//  Copyright © 2017年 mingxin.ji. All rights reserved.
//

#import "RACSignalViewController.h"

@interface RACSignalViewController ()

@property (nonatomic, strong) RACSignal *signal;

@property (nonatomic, strong) id<RACSubscriber> subscriber;

@end

@implementation RACSignalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"RACSignal";
    
    [self setupView];
    
    
}

- (void) setupView{
    
    UIButton *sendBtn = ({
        UIButton *target = [UIButton buttonWithType:UIButtonTypeCustom];
        [target setTitle:@"发送信号" forState:UIControlStateNormal];
        [target setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [target addTarget:self action:@selector(sendSignal) forControlEvents:UIControlEventTouchUpInside];
        target;
    });
    [self.view addSubview:sendBtn];
    [sendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).offset(64);
        make.width.mas_equalTo(self.view.mas_width);
        make.height.mas_equalTo(44);
        make.centerX.mas_equalTo(self.view.mas_centerX);
    }];
    
    UIButton *subscribeBtn = ({
        UIButton *target = [UIButton buttonWithType:UIButtonTypeCustom];
        [target setTitle:@"订阅信号" forState:UIControlStateNormal];
        [target setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [target addTarget:self action:@selector(subscribeSignal) forControlEvents:UIControlEventTouchUpInside];
        target;
    });
    [self.view addSubview:subscribeBtn];
    [subscribeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(sendBtn.mas_bottom).offset(10);
        make.width.mas_equalTo(self.view.mas_width);
        make.height.mas_equalTo(44);
        make.centerX.mas_equalTo(self.view.mas_centerX);
    }];
    
}

// 创建信号
- (void) sendSignal{
    self.signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        NSLog(@"创建信号量");
        
        // 发布信息
        [subscriber sendNext:@"发送的信息"];
        self.subscriber = subscriber;
        NSLog(@"那我啥时候运行");
        
        [self showSuccess:@"信号创建成功"];
        
        return [RACDisposable disposableWithBlock:^{
            NSLog(@"disposable");
        }];
    }];
}

// 订阅信号
- (void) subscribeSignal{
    
    [self.signal subscribeNext:^(id x) {
        NSLog(@"信号传过来的信息-> %@",x);
        
        [self showSuccess:x];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
